﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AteneoLibriBusinessLogic.Entities;
using AteneoLibriBusinessLogic.MyException;

namespace AteneoLibriBusinessLogic.DAL
{
    public class FacultyFacade
    {

        AteneoLibriEntityFramework.AteneoLibriEntities db = new AteneoLibriEntityFramework.AteneoLibriEntities();

        public List<Faculty> getFaculties(Guid universityId)
        {
            List<Faculty> list = new List<Faculty>();
            try
            {
                IEnumerable<AteneoLibriEntityFramework.Faculty> faculties = new List<AteneoLibriEntityFramework.Faculty>();
                faculties = db.Faculties.Where(x => x.UniversityId == universityId).ToList();

                foreach (var item in faculties)
                {
                    Faculty fac = TOA.FacultyAssembler.Assembler(item);
                    list.Add(fac);
                }
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("ERROR: [METHOD: getFaculties()] " + ex.Message);
            }

            return list;
        }

        public Faculty getFaculty(Guid idFaculty)
        {
            AteneoLibriEntityFramework.Faculty facultyEF = db.Faculties.Where(x => x.IdFaculty == idFaculty).FirstOrDefault();
            Faculty faculty = TOA.FacultyAssembler.Assembler(facultyEF);
            return faculty;
        }

        public bool insertFaculty(Faculty faculty)
        {
            bool result = false;
            try
            {
                AteneoLibriEntityFramework.Faculty facultyEF = TOA.FacultyAssembler.DeAssembler(faculty);
                db.Faculties.AddObject(facultyEF);
                db.SaveChanges();

                result = true;
            }
            catch (Exception ex)
            {
                throw new DALException(ex.Message, ex);
            }
            return result;
        }

        public bool updateFaculty(Faculty faculty)
        {
            bool result = false;
            try
            {
                AteneoLibriEntityFramework.Faculty facultyEF = db.Faculties.Where(x => x.IdFaculty == faculty.IdFaculty).FirstOrDefault();
                TOA.FacultyAssembler.WriteToEntity(facultyEF, faculty);

                db.SaveChanges(System.Data.Objects.SaveOptions.AcceptAllChangesAfterSave);
                result = true;
            }
            catch (Exception ex)
            {
                throw new DALException(ex.Message, ex);
            }

            return result;
        }

        public bool deleteFaculty(Faculty faculty)
        {
            bool result = false;
            try
            {
                AteneoLibriEntityFramework.Faculty facultyEF = db.Faculties.Where(x => x.IdFaculty == faculty.IdFaculty).FirstOrDefault();
                db.DeleteObject(facultyEF);                
                db.SaveChanges();

                result = true;
            }
            catch (Exception ex)
            {
                throw new DALException(ex.Message, ex);
            }

            return result;
        }
    }
}
